package com.das.mechanic_base.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.das.mechanic_base.R;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

public class X3BaseDialogUtil extends Dialog {

    private Activity context;
    private int itemLayoutId;
    private List<View> list;

    private Point mScreenPoint = new Point();

    /**
     * 无高度设置、居中显示
     * @param mContext
     * @param view
     * @return
     */
    public static X3BaseDialogUtil showDialog(Activity mContext, int view) {
        X3BaseDialogUtil dialog = new X3BaseDialogUtil(mContext, R.style.pointDialog,view);
        dialog.show();
        return dialog;
    }
    public static X3BaseDialogUtil showDialog(Activity mContext, int view, String str) {
        X3BaseDialogUtil dialog = new X3BaseDialogUtil(mContext, R.style.PhotoDialog, view);
        dialog.show();
        return dialog;
    }
    public static X3BaseDialogUtil showDialog(Activity mContext, int view, boolean flag) {
        X3BaseDialogUtil dialog = new X3BaseDialogUtil(mContext, view);
        dialog.show();
        return dialog;
    }
    /**
     * 距离头部、底部的高度
     * @param mContext
     * @param view
     * @param y
     * @return
     */
    public static X3BaseDialogUtil showDialog(Activity mContext, int view, int mGravity, int y) {
        X3BaseDialogUtil dialog = new X3BaseDialogUtil(mContext, R.style.PhotoDialog, view);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(mGravity);
        lp.y = y; // 新位置Y坐标
        dialogWindow.setAttributes(lp);
        dialog.show();
        return dialog;
    }

    public X3BaseDialogUtil(Activity context, int theme, int itemLayoutId) {
        super(context, theme);
        this.context = context;
        this.itemLayoutId = itemLayoutId;

    }
    public X3BaseDialogUtil(Activity context, int itemLayoutId) {
        super(context);
        this.context = context;
        this.itemLayoutId = itemLayoutId;

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new BitmapDrawable());
        setContentView(itemLayoutId);
        this.setCanceledOnTouchOutside(false);
        initDialogWindow();
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = null;
        view = findViewById(viewId);
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public X3BaseDialogUtil setText(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    @SuppressWarnings("deprecation")
    private void initDialogWindow() {
        Window dialogWindow = getWindow();

        try {
            dialogWindow.setGravity(Gravity.CENTER);
        }catch (Exception e) {
            e.printStackTrace();
        }


        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        try {
            d.getSize(mScreenPoint);
        } catch (NoSuchMethodError ignore) {
            mScreenPoint.x = d.getWidth();
            mScreenPoint.y = d.getHeight();
        }

        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mScreenPoint.x * 0.8);    // 宽度设置为屏幕的0.8
        p.y = - (int) (mScreenPoint.y * 0.007);
        dialogWindow.setAttributes(p);

//        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.alpha = 0.6f;
//        getWindow().setAttributes(lp);
    }

    @Override
    public void setOnDismissListener(@Nullable DialogInterface.OnDismissListener listener) {
        super.setOnDismissListener(listener);
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("取消服务项目弹窗");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        MobclickAgent.onPageEnd("取消服务项目弹窗");
        if (iOnDismiss != null) {
            iOnDismiss.iOnDismiss();
        }
    }

    IOnDismiss iOnDismiss;

    public void setiOnDismiss(IOnDismiss iOnDismiss) {
        this.iOnDismiss = iOnDismiss;
    }

    public interface IOnDismiss {
        void iOnDismiss();
    }
}
